//index.js
const app = getApp();
import updateLcn from '../../utils/updateLcn' //查看/更新当前位置
import navigation from '../../utils/navigation' //导航功能
import searchPoi from '../../utils/searchPoi' //搜索自定poi点
import getMarkers from '../../utils/getMarkers' //获取markers标点
import getSetting from '../../utils/getSetting' //获取位置弹窗
import subway from '../../utils/subway' //地铁图
import routeplan from '../../utils/routeplanning' //路线规划
import voice from '../../utils/voice' //语音
const chooseLoc = requirePlugin('chooseLocation');
const QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min');
let qqmapsdk = new QQMapWX({
	key: 'NMKBZ-SACK6-WFDSR-M7UUJ-Q7OYF-FQB6V' // 必填
});
Page({
	data: {
		pointimg: 'none', //红导航图标默认隐藏
		areaimg: '', //灰导航图标默认显示，无法点击
		list: ['景点名'], //景点列表
		longitude: 0,
		latitude: 0,
		markers: [], //标点
		scale: 18, //视野大小
		mainH: 0, //景点列表的高度
		title: [{ //自定义组件 标签栏内容
			title: '所有景区'
		}, {
			title: '地铁图'
		}, {
			title: '周围景点'
		}, {
			title: '我在哪'
		}, ]
	},
	tabsClick(e) {
		switch (e.detail.detail.index) {
			case 0:
				// console.log(e.detail.detail.index);
				getMarkers.getMarkers.apply(this)
				break;
			case 1:
				// console.log(e.detail.detail.index);
				subway.subways()
				break;
			case 2:
				// console.log(e.detail.detail.index);
				searchPoi.search.apply(this, [qqmapsdk])
				break;
			case 3:
				// console.log(e.detail.detail.index);
				updateLcn.updateLcn.apply(this, [qqmapsdk])
				break;
		}
	},
	//右上角转发给朋友按钮
	onShareAppMessage: function () {},
	//转发到朋友圈
	onShareTimeline() {},
	//详细景点列表的导航按钮
	bindNavigation(e) {
		console.log(e.target.dataset.index); //获取下标
		navigation.navigation.apply(this, [e, qqmapsdk])
	},
	// 景区的导航按钮
	bindscenicArea(e) {
		console.log(e.target.dataset.index); //获取下标
		getMarkers.listplan(e)
	},
	onShow() {
		this.getTabBar().init();
		const location = chooseLoc.getLocation(); // 点击确认选点按钮，则返回选点结果对象，否则返回null
		console.log(location);
		if (location != null) {
			routeplan.planning({
				address: location.address,
				city: location.city,
				longitude: location.longitude,
				latitude: location.latitude,
				name: location.name,
			})
			chooseLoc.setLocation() //chooseLoc值设为空
		}
	},
	onReady() {
		//显示markers
		getMarkers.getMarkers.apply(this)
	},
	onLoad(options) {
		let that = this
		qqmapsdk.reverseGeocoder({
			success: (res) => {
				const {
					lat,
					lng
				} = res.result.location;
				console.log(res.result.location);
				that.setData({
					//测试视角 阳光学院 26.003818,119.448499
					latitude: lat,
					longitude: lng
				});
			},
			fail: (err) => {
				//获取位置授权失败提示
				getSetting.getSetting()
			}
		})

		//系统高度
		if (RegExp(/iPhone/).test(app.globalData.model) && app.globalData.windowHeight >= 724) {
			console.log('高度', app.globalData.windowHeight - 402 - 50 - 34 - 2);
			that.setData({
				mainH: app.globalData.windowHeight - 402 - 50 - 34 - 2
			})
		} else {
			console.log('高度', app.globalData.windowHeight - 404 - 50 - 2);
			that.setData({
				mainH: app.globalData.windowHeight - 402 - 50 - 2
			})
		}
	},
	//点击标点时触发
	markertap(e) {
		console.log(e);
	},
	// 点击标记点对应的气泡时触发e.detail = {markerId}
	callouttap(e) {
		voice.wordYun('查看景点介绍')
		console.log('callouttap', e);
		wx.navigateTo({
			url: '../intro/intro',
		})
	},
	poitap(e) {
		console.log('poitap', e.detail);
	}
});